﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class PerformanceController :UI.BaseController
    {
        protected override bool IsPerformance { get { return false; } }

        public const string ProgrameCode = "Performance";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public PerformanceController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public ActionResult Index()
        {
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]
        public string List(int page, int rows, string sort, string order)
        {
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.Performance), "ControllerName,ActionName,ElapsedMilliseconds,Time");
            string orderby = "ID desc";
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;

            var bll = new BLL.Performance();
            var list = bll.Get(where, orderby, (page - 1) * rows, rows);
            List<object> listView = new List<object>();
            foreach (var i in list)
            {
                listView.Add(new
                {
                    ID = i.ID,
                    ControllerName = i.ControllerName,
                    ActionName = i.ActionName,
                    ElapsedMilliseconds = i.ElapsedMilliseconds,
                    PathAndQuery = i.PathAndQuery,
                    Time = UI.Utils.FormateDate(i.Time)
                });
            }
            var ret = new
            {
                total=bll.GetCount(where),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete")]
        public string Delete(string id)
        {
            var ret = new BLL.Performance().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

    }
}
